#!/bin/sh

TELECOM_SD=`lsusb | grep 19d2:2000`
MODEM=`lsusb | grep 19d2:0031`

if [ -z "$TELECOM_SD" -a -z "$MODEM" ]; then 
	echo "\nT-Stick is not plugged in. \n"
	exit
fi

if [ -n "$TELECOM_SD" ]; then
	
	sleep 5
	echo "\nEjecting Telecom storage device... \n"

	sudo eject /dev/sr0
	sleep 3

	# Make sure it ejected
	SR=`ls /dev/ | grep sr0`
	while [ -n "$SR" ]; do
	        sudo eject /dev/sr0
		sleep 3
	        SR=`ls /dev/ | grep sr0`
	done

	# Wait for modem to be recognized
	while [ -z "$MODEM" ]; do
		sleep 1
	        MODEM=`lsusb | grep 19d2:0031`
	done
	sleep 6
	echo "\nModem connected. \n"
else 
	echo "\nModem already connected. \n"
	exit 0
fi

